#include "DBMgr.h"

DBMgr* DBMgr::_instance = new DBMgr();
DBMgr* DBMgr::GetInstance()
{
	return _instance;
}

DBMgr::DBMgr( )
{
}

DBMgr::~DBMgr( )
{
}

int DBMgr::serachUserInDB(char *namestr)
{
	int result = -1;

	return result;
}

int DBMgr::loadCof(char *filename)
{
	FILE *f=fopen(filename,"rb");

	if(f==nullptr)
	{
		std::cout<< "no file:"<<filename<<std::endl;

		exit(0);
	}

	fseek(f,0,SEEK_END);
	long len=ftell(f);
	fseek(f,0,SEEK_SET);
	char *data=(char*)malloc(len+1);
	fread(data,1,len,f);
	fclose(f);
	decodeJsonStr(data);
	free(data);

	return 0;
}

void DBMgr::decodeJsonStr(char *text)
{
	cJSON *json;

	json=cJSON_Parse(text);
	if (!json)
	{
		printf("mysqlcenter Error before: [%s]\n",cJSON_GetErrorPtr());
	}
	else
	{
		cJSON *c=cJSON_GetObjectItem(json,"dbUser");
		memset(MySqlCenter::GetInstance()->_dbuser,0,sizeof(128));
		strcpy(MySqlCenter::GetInstance()->_dbuser,c->valuestring);

		c=cJSON_GetObjectItem(json,"dbPasswd");
		strcpy(MySqlCenter::GetInstance()->_dbpasswd,c->valuestring);

		c=cJSON_GetObjectItem(json,"dbname");
		strcpy(MySqlCenter::GetInstance()->_dbname ,c->valuestring);

		c=cJSON_GetObjectItem(json,"tablename");
		strcpy(MySqlCenter::GetInstance()->_tablename,c->valuestring);

		c=cJSON_GetObjectItem(json,"dbIp");
		strcpy(MySqlCenter::GetInstance()->_dbip,c->valuestring);

		c=cJSON_GetObjectItem(json,"port");
		MySqlCenter::GetInstance()->_port = c->valueint;

		c=cJSON_GetObjectItem(json,"t_RoleName");
		strcpy(t_RoleName,c->valuestring);

		c=cJSON_GetObjectItem(json,"t_Account");
		strcpy(t_Account,c->valuestring);

		cJSON_Delete(json);
	}
}